package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.dao.CourseDao;
import com.wanke.entity.Banji;
import com.wanke.entity.Course;

/**
 * Home object for domain model class Course.
 * @see com.wanke.entity.Course
 * @author Hibernate Tools
 */
@Repository("courseDao")
public class CourseDaoImpl implements CourseDao {

	private static final Log log = LogFactory.getLog(CourseDaoImpl.class);

	@Resource(name="sessionFactory")
	 SessionFactory sessionFactory ;

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#persist(com.wanke.entity.Course)
	 */
	@Override
	public void persist(Course transientInstance) {
		log.debug("persisting Course instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#attachDirty(com.wanke.entity.Course)
	 */
	@Override
	public void attachDirty(Course instance) {
		log.debug("attaching dirty Course instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#attachClean(com.wanke.entity.Course)
	 */
	@Override
	public void attachClean(Course instance) {
		log.debug("attaching clean Course instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#delete(com.wanke.entity.Course)
	 */
	@Override
	public void delete(Course persistentInstance) {
		log.debug("deleting Course instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#merge(com.wanke.entity.Course)
	 */
	@Override
	public Course merge(Course detachedInstance) {
		log.debug("merging Course instance");
		try {
			Course result = (Course) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#findById(int)
	 */
	@Override
	public Course findById(int id) {
		log.debug("getting Course instance with id: " + id);
		try {
			Course instance = (Course) sessionFactory.getCurrentSession().get("com.wanke.entity.Course", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CourseDao#findByExample(com.wanke.entity.Course)
	 */
	@Override
	public List findByExample(Course instance) {
		log.debug("finding Course instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.Course")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	//根据当前时间和班级id来查询当前的课程是哪一节
	public Course clazzCheck(int banji_id,String begin,String zhouji){
		Course cous;
		try{
			String hql="from Course where banji_id=? and begin_order=? and weekday=?";
			Query query=sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, banji_id);
			query.setString(1, begin);
			query.setString(2, zhouji);
			if(query.list().size()>0){
			cous=(Course) query.list().get(0);
			}else{
				cous=null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return cous;
	}
	
	
}
